<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

/**
 * Loobica Web Core Controller
 * 
 * @author    Dino (DBK)
 * @date      1 Jan 2014
 * 
 */
class LB_Controller extends CI_Controller {

  public $signed_user = array();
  public $location_data = array();
  public $session_id = null;
  public $redirect_to = null;

  public function __construct() {
    parent::__construct();

    // Set headers
    $this->output->set_header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
    $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
    $this->output->set_header('Pragma: no-cache');

    // Loading cache driver
    $this->load->driver('cache', array('adapter' => 'file'));

    // Set signed user
    $this->signed_user = $this->user_session->signed_user();

    // Getting user location
    $this->location_data = $this->user_location->get();

    // Setting session id
    $this->session_id = $this->loobica_hash($this->input->ip_address());

    // Loading current language
    $this->load_language();

    // Settting redirect to url
    $this->set_redirect_to();
  }

  /**
   * Encrypt password
   * 
   */
  public function loobica_hash($value = null) {
    return (string) md5($value . STATIC_SALT);
  }

  /**
   * Check the request type is get
   * 
   */
  public function is_get() {
    if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET') {
      return true;
    } else {
      return false;
    }
  }

  /**
   * Check the request type is post
   * 
   */
  public function is_post() {
    if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
      return true;
    } else {
      return false;
    }
  }

  /**
   * Check the request type is ajax
   * 
   */
  public function is_ajax() {
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
      return true;
    } else {
      return false;
    }
  }

  /**
   * Load header & footer view
   * 
   */
  public function load_view($view = null, $vars = array()) {

    // Render asset files
    $this->render_assets();

    // Load view
    if ($this->is_ajax()) {
      if ($this->is_get()) {
        if (!empty($vars['title'])) {
          $title = $vars['title'] . ' - ' . lang('loobica') . ' - ' . lang('next_gen_ads');
          $this->common_view->render_url_title(current_full_url(true), $title);
        } else {
          $title = lang('loobica') . ' - ' . lang('next_gen_ads');
          $this->common_view->render_url_title(current_full_url(true), $title);
        }
      }
      $this->common_view->render_navbar($vars);
      $this->common_view->render_sidebar($vars);
      $this->load->view($view, $vars);
    } else {
      $this->load->view('common/common', array('view' => $view, 'vars' => $vars));
    }
  }

  /**
   * Set the previous page of user
   * 
   */
  public function set_redirect_to() {
    if ($this->is_post()) {
      $this->redirect_to = $this->input->post('redirect_to', true);
    } else {
      $this->redirect_to = $this->input->get('redirect_to', true);
    }
  }

  /**
   * Set language in cookie
   * 
   */
  public function set_language($language = 'english') {
    $language = (string) trim((in_array($language, $this->config->item('languages'))) ? $language : 'english');
    return set_cookie('loobica_language', (string) $language, time() + (20 * 365 * 24 * 60 * 60));
  }

  /**
   * Load language
   * 
   */
  public function load_language() {
    $cookie_language = get_cookie('loobica_language', true);
    $user_language = (string) trim((in_array($cookie_language, $this->config->item('languages'))) ? $cookie_language : 'english');
    $this->lang->load('web', $user_language);
  }

  /**
   * Minify & Generate CSS & JS Files
   * 
   */
  public function render_assets() {

    // Preparing paths & files
    $asset_key = $this->config->item('asset_key');
    $asset_path = $this->config->item('asset_path');
    $css_file = $asset_path . $asset_key . '.css';
    $js_file = $asset_path . $asset_key . '.js';

    // Checking file exists 
    if ((!file_exists($css_file)) || (!file_exists($js_file))) {

      // Clearing previous files
      $files = glob($asset_path . '*');
      foreach ($files as $file) {
        if (is_file($file)) {
          unlink($file);
        }
      }

      // Generating new assets files
      $this->load->library('minify/minify');
      $css_content = $this->minify->combine_directory('assets/web/css/');
      $jss_content = $this->minify->combine_directory('assets/web/jss/');
      $this->minify->save_file($css_content, $css_file);
      $this->minify->save_file($jss_content, $js_file);

      // Minified assets generated
      return true;
    }

    // Minified assets already exists
    return false;
  }

  /**
   * Prevent showing page for signed users
   * 
   */
  public function prevent_signed() {
    if (!empty($this->signed_user)) {
      $this->redirect();
    }
  }

  /**
   * Prevent showing page for unsigned users
   * 
   */
  public function prevent_unsigned() {
    if (empty($this->signed_user)) {
      $this->redirect();
    }
  }

  /**
   * Perform redirects
   * 
   */
  public function redirect($url = null) {
    if ($this->is_ajax()) {
      redirect($url, 'ajax');
    } else {
      redirect($url);
    }
  }

  /**
   * Send email
   * 
   */
  public function send_email($email = null, $subject = null, $content = null) {
    $this->email->clear(true);
    $this->email->set_mailtype('html');
    $this->email->from($this->config->item('smtp_user'), 'Loobica Ads');
    $this->email->to($email);
    $this->email->subject($subject);
    $this->email->message($content);
    $this->email->send();
    return;
  }

}

/**
 * Auth Controller
 * 
 */
class LB_Auth_Controller extends LB_Controller {

  public function __construct() {
    parent::__construct();
    if (!empty($this->signed_user)) {
      // User signed in
    } else {
      // User not signed in
      $this->signed_user = array();
      $this->user_session->sign_out();
      if (!empty($this->redirect_to)) {
        $this->redirect('signin?redirect_to=' . $this->redirect_to);
      } else {
        $this->redirect('signin');
      }
    }
  }

}

?>